# -*- coding: utf-8 -*-
# 文件: animated_toolbox.py
# 功能: 带动画展开、图标+文字居中、阴影浮起的自定义 Accordion ToolBox 控件
# ----------- AnimatedToolBox 控件 -----------

from PySide6 import QtWidgets, QtCore, QtGui
from functools import partial

class AnimatedToolBox(QtWidgets.QWidget):
    """
    带动画展开 + 弹簧效果的 Accordion 风格工具箱
    - 点击按钮展开页面紧跟按钮
    - 底部弹簧自动填充剩余空间
    """
    def __init__(self, parent=None):
        super().__init__(parent)

        self.layout = QtWidgets.QVBoxLayout(self)
        self.layout.setSpacing(4)
        self.layout.setContentsMargins(2, 2, 2, 2)

        self.buttons = []
        self.pages = []

        # 底部弹簧
        self.spacer = QtWidgets.QSpacerItem(0, 0, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.layout.addItem(self.spacer)

    def addItem(self, widget: QtWidgets.QWidget, text: str, icon: QtGui.QIcon = None):
        """添加按钮 + 页面"""
        btn = QtWidgets.QPushButton(text)
        btn.setCheckable(True)
        btn.setMinimumHeight(72)
        btn.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
        btn.setStyleSheet(self.button_style())
        if icon:
            btn.setIcon(icon)
            btn.setIconSize(QtCore.QSize(32, 32))

        btn.clicked.connect(partial(self.select, btn))

        # 插入按钮在弹簧前
        self.layout.insertWidget(self.layout.count() - 1, btn)
        self.buttons.append(btn)

        # 页面紧跟按钮
        widget.setMaximumHeight(0)
        widget.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
        self.layout.insertWidget(self.layout.count() - 1, widget)
        self.pages.append(widget)

        # 默认展开第一个
        if len(self.buttons) == 1:
            btn.setChecked(True)
            self.animate_page(widget)

    def select(self, btn: QtWidgets.QPushButton):
        """点击按钮展开对应页面，其它页面收缩"""
        for i, b in enumerate(self.buttons):
            page = self.pages[i]
            if b == btn:
                b.setChecked(True)
                self.animate_page(page)
            else:
                b.setChecked(False)
                self.collapse_page(page)

    def animate_page(self, page: QtWidgets.QWidget):
        """展开页面动画"""
        target_height = page.sizeHint().height()
        anim = QtCore.QPropertyAnimation(page, b"maximumHeight")
        anim.setDuration(260)
        anim.setStartValue(page.maximumHeight())
        anim.setEndValue(target_height)
        anim.setEasingCurve(QtCore.QEasingCurve.OutCubic)
        anim.start()
        page._animation = anim

    def collapse_page(self, page: QtWidgets.QWidget):
        """收缩页面动画"""
        anim = QtCore.QPropertyAnimation(page, b"maximumHeight")
        anim.setDuration(200)
        anim.setStartValue(page.maximumHeight())
        anim.setEndValue(0)
        anim.setEasingCurve(QtCore.QEasingCurve.OutCubic)
        anim.start()
        page._animation = anim

    @staticmethod
    def button_style():
        return """
        QPushButton {
            background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                                        stop:0 #F4ECE5, stop:0.45 #EBDFD6, stop:1 #DECCBF);
            color: #6D4C41;
            border: 1.8px solid #BB9A89;
            border-radius: 6px;
            padding: 16px 18px;
            font: 14pt "等线";
            text-align: center;
        }
        QPushButton:hover {
            background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                                        stop:0 #EDE2D9, stop:0.45 #E2D3C9, stop:1 #D3BDAF);
            color: #5B3E34;
        }
        QPushButton:checked {
            background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                                        stop:0 #C5A38F, stop:0.45 #AD8874, stop:1 #8A6A57);
            color: white;
            font-weight: 600;
        }
        """

